package com.wfm.leetcode.editor.cn;
public class P剑指Offer36二叉搜索树与双向链表{
class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    Node head,pre= null;
    public Node treeToDoublyList(Node root) {
        if (root==null) return head;
        dfs(root);
        pre.right=head;
        head.left=pre;
        return head;
    }
    public void dfs(Node cur){
        if(cur==null) return;
        dfs(cur.left);
        if(pre!=null){
            pre.right=cur;
        }else{
            head=cur;
        }
        cur.left=pre;
        pre=cur;
        dfs(cur.right);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new P剑指Offer36二叉搜索树与双向链表().new Solution();
    }
}